package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Physical;
import com.itheima.service.PhysicalService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/physical")
public class PhysicalController {

    @Reference
    private PhysicalService physicalService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        return physicalService.findPage(pageBean);
    }

    //分页查询
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('PHYSICAL_ADD')")
    public Result add(@RequestBody Physical physical){
        try {
            physicalService.add(physical);
            return new Result(true,MessageConstant.ADD_PHYSICAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_PHYSICAL_FAIL);
        }
    }


    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('PHYSICAL_EDIT')")
    public Result edit(@RequestBody Physical physical){
        try {
            physicalService.edit(physical);
            return new Result(true,MessageConstant.EDIT_PHYSICAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_PHYSICAL_FAIL);
        }
    }

    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('PHYSICAL_DELETE')")
    public Result delete(Integer id){
        try {
            physicalService.delete(id);
            return new Result(true,MessageConstant.DELETE_PHYSICAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_PHYSICAL_FAIL);
        }
    }
    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('PHYSICAL_QUERY')")
    public Result findById(Integer id){
        try {
            Map map =physicalService.findById(id);
            return new Result(true,MessageConstant.QUERY_PHYSICAL_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_PHYSICAL_FAIL);
        }
    }
}
